﻿using FarmSmartSleep.Common.Domain.PM;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using Org.BouncyCastle.Asn1.Ocsp;

namespace FarmSmartSleep.Organ.API.Application.ProductionManagement.MotherHybridizationCommands
{
    public class MotherHybridizationUpdateHander : IRequestHandler<MotherHybridizationUpdateCommand, Result>
    {

        private readonly IBaseRepository<MotherHybridization> baseRepository;
        private readonly ILogger<MotherHybridizationUpdateHander> logger;

        public MotherHybridizationUpdateHander(IBaseRepository<MotherHybridization> baseRepository, ILogger<MotherHybridizationUpdateHander> logger)
        {
            this.baseRepository = baseRepository;
            this.logger = logger;
        }
        /// <summary>
        /// 修改配种信息
        /// </summary>
        /// <param name="request">修改配种信息请求对象</param>
        /// <param name="cancellationToken"></param>
        /// <returns>修改配种信息响应对象</returns>
        public async Task<Result> Handle(MotherHybridizationUpdateCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new Result();
                var info = baseRepository.GetAll().FirstOrDefault(c => c.MotherHybridizationId == request.MotherHybridizationId);
                if (info != null)
                {
                    info.MotherHybridizationId = request.MotherHybridizationId;
                    info.MotherSleepInforId =   request.MotherSleepInforId; 
                    info.FatherSleepInforId = request.FatherSleepInforId;
                    info.HybridizationTypeId = request.HybridizationTypeId;
                    info.EstrusTypeId = request.EstrusTypeId;
                    info.Mark = request.Mark;
                    info.ToCottageId = request.ToCottageId;
                    info.ToFieIdId = request.ToFieIdId;
                    info.DocumentNumber = request.DocumentNumber;
                    info.ChargePerson= request.ChargePerson;
                    info.UpdateId = request.UpdateId;
                    info.UpdateTime=DateTime.Now;
                    result.Code = baseRepository.Update(info);
                }
                return result;
            }
            catch (Exception ex )
            {

                throw;
            }
        }
    }
}
